package zhihuilan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import zhihuilan.common.WxMiniResponse;
import zhihuilan.dto.MyFactoryProductDto;
import zhihuilan.dto.WxMiniPageDto;
import zhihuilan.entity.MyCart;
import zhihuilan.entity.MyFactoryProduct;
import zhihuilan.entity.MyShop;
import zhihuilan.mapper.MyCartMapper;
import zhihuilan.mapper.MyCircleCollectionMapper;
import zhihuilan.mapper.MyFactoryProductMapper;
import zhihuilan.params.query.MyFactoryProductQueryParam;
import zhihuilan.params.query.ProductQueryParam;
import zhihuilan.service.MyFactoryProductService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 用户数据 服务实现
 *
 * @author ruoyi
 */
@Service
public class MyFactoryProductServiceImpl extends ServiceImpl<MyFactoryProductMapper, MyFactoryProduct> implements MyFactoryProductService {

    @Resource
    private MyCartMapper myCartMapper;

    @Override
    public MyFactoryProduct selectById(Integer id) {
        if (ObjectUtils.isEmpty(id)) {
            return null;
        }
        MyFactoryProduct result = this.baseMapper.selectById(id);
        return result;
    }

    @Override
    public List<MyFactoryProduct> selectList(MyFactoryProductQueryParam queryParam) {
        if (ObjectUtils.isEmpty(queryParam)) {
            return null;
        }
        QueryWrapper<MyFactoryProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        if (!ObjectUtils.isEmpty(queryParam.getShopid())) {
            queryWrapper.eq("shopid", queryParam.getShopid());
        }
        if (!ObjectUtils.isEmpty(queryParam.getPid1())) {
            queryWrapper.eq("pid1", queryParam.getPid1());
        }
        if (!ObjectUtils.isEmpty(queryParam.getPid2())) {
            queryWrapper.eq("pid2", queryParam.getPid2());
        }
        if (!ObjectUtils.isEmpty(queryParam.getPid3())) {
            queryWrapper.eq("pid3", queryParam.getPid3());
        }
        if (!ObjectUtils.isEmpty(queryParam.getPid4())) {
            queryWrapper.eq("pid4", queryParam.getPid4());
        }
        if (!ObjectUtils.isEmpty(queryParam.getPid5())) {
            queryWrapper.eq("pid5", queryParam.getPid5());
        }
        queryWrapper.orderByAsc("addtime");
        List<MyFactoryProduct> list = this.baseMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public MyFactoryProduct selectOne(MyFactoryProductQueryParam queryParam) {
        if (ObjectUtils.isEmpty(queryParam)) {
            return null;
        }
        QueryWrapper<MyFactoryProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        if (!ObjectUtils.isEmpty(queryParam.getShopid())) {
            queryWrapper.eq("shopid", queryParam.getShopid());
        }
        if (!ObjectUtils.isEmpty(queryParam.getPid1())) {
            queryWrapper.eq("pid1", queryParam.getPid1());
        }
        if (!ObjectUtils.isEmpty(queryParam.getPid2())) {
            queryWrapper.eq("pid2", queryParam.getPid2());
        }
        if (!ObjectUtils.isEmpty(queryParam.getPid3())) {
            queryWrapper.eq("pid3", queryParam.getPid3());
        }
        if (!ObjectUtils.isEmpty(queryParam.getPid4())) {
            queryWrapper.eq("pid4", queryParam.getPid4());
        }
        if (!ObjectUtils.isEmpty(queryParam.getPid5())) {
            queryWrapper.eq("pid5", queryParam.getPid5());
        }
        queryWrapper.orderByAsc("addtime");
        MyFactoryProduct one = this.baseMapper.selectOne(queryWrapper);
        return one;
    }

    @Override
    public Map selectSearchProductPage(ProductQueryParam productQueryParam) {
        String field = "", order = "", w = "";
        if (!ObjectUtils.isEmpty(productQueryParam.getFactoryname())) {
            productQueryParam.setFactoryname(productQueryParam.getFactoryname().replaceAll(" ", ""));
            w += " and( s.title like '%" + productQueryParam.getFactoryname()+"%' or s.title1 like  '%" + productQueryParam.getFactoryname()+"%')";

        }
        if (!ObjectUtils.isEmpty(productQueryParam.getDistrictid())) {
            w += " and s.districtid = " + productQueryParam.getDistrictid();
        }
        if (!ObjectUtils.isEmpty(productQueryParam.getPid3name())) {
            productQueryParam.setPid3name(productQueryParam.getPid3name().trim().replaceAll(" ", "-").replaceAll("_", "-").replaceAll("/", ""));
            field += ",  LOCATE('" + productQueryParam.getPid3name() + "', a.pid3name) as a3l";
            order += "a3l asc,CHAR_LENGTH(pid3name) asc,";
            w += " and a.pid3name like '%" + productQueryParam.getPid3name() + "%'";

        }
        if (!ObjectUtils.isEmpty(productQueryParam.getPid5name())) {
            productQueryParam.setPid5name(productQueryParam.getPid5name().replaceAll(" ", "").replaceAll("\\*", "X").replaceAll("x", "X").replaceAll("×", "X").replaceAll("✘", "X").replaceAll("＊", "X"));
            field += ",  LOCATE('" + productQueryParam.getPid5name() + "', a.pid5name) as a5l";
            order += "a5l asc,CHAR_LENGTH(pid5name) asc,";
            w += " and a.pid5name like '%" + productQueryParam.getPid5name() + "%'";
        }

        if (!ObjectUtils.isEmpty(productQueryParam.getPid2name())) {
            productQueryParam.setPid2name(productQueryParam.getPid2name().replaceAll(" ", ""));
            w += " and ( a.pid2name like '%" + productQueryParam.getPid2name() + "%' or a.pid1name like '%" + productQueryParam.getPid2name() + "%')";
            order += "a.pid2name asc,a.pid1name asc,";
        }

        if (!ObjectUtils.isEmpty(productQueryParam.getPid1name())) {
            productQueryParam.setPid1name(productQueryParam.getPid1name().replaceAll(" ", ""));
            w += " and a.pid1name like '%" + productQueryParam.getPid1name() + "%'";
            order += "a.pid1name asc,";
        }
        List<MyFactoryProduct> list = baseMapper.selectSearchProduct((productQueryParam.getPage() - 1) * productQueryParam.getPageSize(), productQueryParam.getPageSize(), field, w, order);
        Long total = baseMapper.selectSearchProductCount(w);
        Map map = new HashMap<>();
        map.put("data", list);
        map.put("total", total);
        return map;

    }

    @Override
    public  Map selectProductByShopIdKeywordPage(MyFactoryProductQueryParam queryParam){
        String where = "";
        if(queryParam.getKeyword()!=null&&!queryParam.getKeyword().isEmpty()) {
            String keyword = queryParam.getKeyword().replaceAll("\\*", "X").replaceAll("x", "X").replaceAll("×", "X").replaceAll("✘", "X").replaceAll("＊", "X");
            String[] arr = keyword.split(" ");
            if (arr.length > 0) {
                for (String k : arr) {
                    if (arr.length > 1) {
                        where += " or";
                    }
                    where += " a.pid1name like '%" + k.trim() + "%' or a.pid2name like '%" + k.trim() + "%' or a.pid3name like '%" + k.trim() + "%'  or a.pid5name like '%" + k.trim() + "%' ";
                }
            }
        }
        List<MyFactoryProduct> list = baseMapper.selectProductByShopIdKeywordList((queryParam.getPage() - 1) * queryParam.getPageSize(), queryParam.getPageSize(), queryParam.getShopid(), where);
        Long total = baseMapper.selectProductByShopIdKeywordCount(queryParam.getShopid(), where);
        Map map = new HashMap<>();
        map.put("data", list);
        map.put("total", total);
        map.put("last_page",(int)Math.ceil(total.doubleValue()/ queryParam.getPageSize().doubleValue()));
        return map;
    }



    @Override
    public MyFactoryProduct addEntity(MyFactoryProduct entity) {
        if (ObjectUtils.isEmpty(entity)) {
            return null;
        }
        if (!ObjectUtils.isEmpty(entity.getId())) {
            return null;
        }
        this.baseMapper.insert(entity);
        return entity;
    }

    @Override
    public MyFactoryProduct updateEntity(MyFactoryProduct entity) {
        if (ObjectUtils.isEmpty(entity)) {
            return null;
        }
        if (ObjectUtils.isEmpty(entity.getId())) {
            return null;
        }
        this.baseMapper.updateById(entity);
        return entity;
    }

    @Override
    public Boolean deleteById(Integer id) {
        if (ObjectUtils.isEmpty(id)) {
            return false;
        }
        int count = this.baseMapper.deleteById(id);
        QueryWrapper<MyCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productid", id);
        count += this.myCartMapper.delete(queryWrapper);
        return count > 0 ? true : false;
    }

    @Transactional
    @Override
    public Boolean updateList(List<MyFactoryProduct> list){
        if(ObjectUtils.isEmpty(list)) {
            return false;
        }
        if(list.size()==0) {
            return false;
        }
        return saveOrUpdateBatch(list);
    }

}
